<script setup>
  import { ref, computed, reactive } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import taskApi from '@/apis/task'
  // 组件 ref
  const popup = ref(null)
  // 异常日期
  const timePicker = ref('')
  // 任务id
  const transportTaskId = ref('')
  // 上报位置
  const exceptionPlace = ref('')
  // 异常类型
  const exceptionType = ref('')
  // 异常描述
  const exceptionDescribe = ref('')
  // 异常现场照片
  const exceptionImagesList = ref([])
  // 异常类型集合
  const exceptionTypes = reactive([
    { text: '发动机启动困难', checked: true },
    { text: '不着车，漏油', checked: false },
    { text: '照明失灵', checked: false },
    { text: '排烟异常、温度异常', checked: false },
    { text: '其他问题', checked: false },
  ])
  // 获取任务id
  onLoad((params) => {
    transportTaskId.value = params.transportTaskId
  })
  // 文件提示
  const exceptTime = computed(() => {
    return timePicker.value || '请选择'
  })
  // 打开地图
  async function onLocationChoose() {
    // 使用uni.getLocation({})来获取经纬度
    let { latitude, longitude } = await uni.getLocation({})
    const { address } = await uni.chooseLocation({ latitude, longitude })
    exceptionPlace.value = address
  }
  // 点击类型弹层确认按钮
  function onPopupConfirm() {
    popup.value.close()
  }
  // 打开弹层
  function onPopupOpen() {
    popup.value.open()
  }
  // 异常类型的点击事件
  function oncheckbox(index) {
    // 根据索引修改exceptionTypes里的checked
    exceptionTypes[index].checked = !exceptionTypes[index].checked
  }
  // 计算异常描述的字数
  const descLength = computed(() => {
    return exceptionDescribe.value.length
  })
  // 计算过滤exceptionTypes中为true的
  const trueTypes = computed(() => {
    // 先过滤出点击了true的
    let trueTypesList = exceptionTypes.filter((item) => item.checked)
    // 再过滤出true的text，因为需要返回文本
    let TypsString = trueTypesList.map((item) => item.text).join('|')
    return TypsString
  })
  // 提交按钮
  async function onsubmit() {
    try {
      const fromData = {
        transportTaskId: transportTaskId.value,
        exceptionTime: timePicker.value,
        exceptionPlace: exceptionPlace.value,
        exceptionType: trueTypes.value,
        exceptionDescribe: exceptionDescribe.value,
        exceptionImagesList: exceptionImagesList.value,
      }
      const { code, msg } = await taskApi.except(fromData)
      if (code !== 200) {
        return uni.utils.toast(msg)
      }
      // reLaunch和switchTab的区别，清空源码栈的所有页面，重新打开一个新页面
      uni.reLaunch({
        url: '/pages/task/index',
      })
    } catch (err) {}
  }
</script>

<template>
  <view class="page-container">
    <scroll-view class="scroll-view" scroll-y>
      <view style="margin: 30rpx" class="scroll-view-wrapper">
        <uni-list :border="false">
          <uni-list-item show-arrow title="异常时间">
            <template v-slot:footer>
              <uni-datetime-picker v-model="timePicker">
                <view class="picker-value">{{ exceptTime }}</view>
              </uni-datetime-picker>
            </template>
          </uni-list-item>

          <uni-list-item
            show-arrow
            clickable
            ellipsis="1"
            @click="onLocationChoose"
            title="上报位置"
            :right-text="exceptionPlace || '请选择'"
          />
          <uni-list-item
            show-arrow
            clickable
            @click="onPopupOpen"
            title="异常类型"
            :rightText="trueTypes || '请选择'"
          />
          <uni-list-item direction="column" title="异常描述">
            <template v-slot:footer>
              <view class="textarea-wrapper">
                <textarea
                  class="textarea"
                  v-model="exceptionDescribe"
                  placeholder="请输入异常描述"
                ></textarea>
                <view :class="{ error: descLength > 50 }" class="words-count"
                  >{{ descLength }}/50</view
                >
              </view>
            </template>
          </uni-list-item>
          <uni-list-item
            :border="false"
            style="margin-top: -30rpx"
            direction="column"
            title="上传图片（最多6张）"
          >
            <template v-slot:footer>
              <uni-file-picker
                v-model="exceptionImagesList"
                limit="6"
              ></uni-file-picker>
            </template>
          </uni-list-item>
        </uni-list>
      </view>
    </scroll-view>
    <view class="fixbar">
      <button class="button disable" @click="onsubmit">提交</button>
    </view>
    <!-- 异常类型弹层 -->
    <uni-popup ref="popup" :mask-click="false" type="bottom">
      <uni-list class="popup-action-sheet">
        <uni-list-item>
          <template v-slot:header>
            <view class="header">选择类型</view>
          </template>
        </uni-list-item>
        <uni-list-item
          v-for="(item, index) in exceptionTypes"
          :key="item.text"
          :title="item.text"
        >
          <template v-slot:footer>
            <checkbox-group @change="oncheckbox(index)" class="checkbox">
              <checkbox
                :value="item.text"
                :checked="item.checked"
                color="#EF4F3F"
              />
            </checkbox-group>
          </template>
        </uni-list-item>

        <uni-list-item>
          <template v-slot:body>
            <button @click="onPopupConfirm" class="button">确定</button>
          </template>
        </uni-list-item>
      </uni-list>
    </uni-popup>
  </view>
</template>

<style lang="scss" scoped>
  @import './index.scss';
</style>
